<template>
  <div class="w_h_100">
    <div class="box">
      <div class="inlet">系统管理 > 权限管理 > <span class="size">部门管理</span></div>
      <myform :fields="fields" :model="values" class="m_t_10" :options="options">
        <template v-slot:button>
          <el-button @click="resetting">重置</el-button>
          <el-button type="primary" :icon="Search" @click="searchFor">搜索</el-button>
        </template>
      </myform>
    </div>
    <div class="mytable m_t_10 ">
      <div class="d_f j_c_s_b a_i_c">
        <h4>部门明细列表</h4>
        <el-button type="primary" :icon="Plus" @click="addDept" v-hasPerm="'addDept'">新增部门</el-button>
      </div>
      <mytable :fields="tableFields" width="100" ref="tableRef" :values="values" api="deptListByPage" class="m_t_10"
        :isSelection="false">
        <template v-slot:isBiz="{ row }">{{ row.isBiz == 0 ? '否' : '是' }}</template>
        <template v-slot:deleted="{ row }">
          <el-switch v-model="row.deleted" inline-prompt active-text="开" inactive-text="关" :active-value="0"
            :inactive-value="1"  @change="changeSwitch(row.id, row.deleted)" style="--el-switch-on-color:#13ce66; " />
        </template>
        <template v-slot:operation="{ row }">
          <div class="d_f j_c_s_b">
            <span class="edit" @click="edit(row)" v-hasPerm="'editDept'">编辑</span>
            <span class="error" @click="deleteOut(row)" v-hasPerm="'deleteDept'">删除</span>
          </div>
        </template>
      </mytable>
    </div>
    <el-drawer v-model="show" :title="title" size="500px" direction="rtl" :before-close="handleClose">
      <div class="dept">
        <myform :fields="addFields" :options="options" ref="ruleFrom" :model="model" :inline="false" labelPosition="top"
          :rules="rules" class="m_t_16"></myform>
      </div>
      <div class="d_f j_c_c">
        <el-button type="primary" @click="save" v-myRepeat>保存</el-button>
        <el-button @click="handleClose">取消</el-button>
      </div>
    </el-drawer>
  </div>
</template>

<script lang='ts'>
import { defineComponent, reactive, toRefs, onBeforeMount, onMounted, ref } from 'vue'
import { authorityDeptType } from '../../../../type/system'
import { copy, objectMatching } from '../../../../utils/date'
import { Search, Plus } from '@element-plus/icons-vue'
import { ElMessageBox } from 'element-plus'
import { deptAdd, deptUpdate, deptDel, deptStatus } from '../../../../api/system/authority'
import { Message } from '../../../../utils/resetMessage'
const values = {
  name: '',
  deleted: null,
}
const model = {
  name: '',//部门名称
  shortName: '',//部门简称
  isBiz: '1',//0-否，1-是
  status: 0,//0-启用，1-禁用
  id: 0,
}
export default defineComponent({
  name: '',
  setup() {
    const data: authorityDeptType = reactive({
      fields: [
        {
          label: "部门名称",
          prop: 'name',
          placeholder: '请输入',
          type: 'input',
          labelWidth: '70px'
        },
        {
          label: "部门状态",
          prop: 'deleted',
          placeholder: '请选择',
          type: 'select',
          bindLabel: 'name',
          bindValue: 'id',
        },
        {
          prop: 'button'
        }
      ],
      options: {
        deleted: [
          {
            name: '启用',
            id: '0',
          },
          {
            name: '禁用',
            id: '1',
          },
        ],
        isBiz: [
          {
            value: '1',
            label: '是',
          },
          {
            value: '0',
            label: '否',
          },
        ],
      },
      tableFields: [
        {
          label: '部门名称',
          prop: 'name',
        },
        {
          label: '是否业务部门',
          prop: 'isBiz',
        },
        {
          label: '创建人',
          prop: 'createdName',
        },
        {
          label: '创建时间',
          prop: 'createdTime',
        },
        {
          label: '部门状态',
          prop: 'deleted',
        },
      ],
      values: copy(values),
      model: copy(model),
      show: false,
      title: '新增部门',
      addFields: [
        {
          label: '部门名称',
          type: 'input',
          placeholder: '请输入部门名称',
          prop: 'name'
        },
        {
          label: '部门简称',
          type: 'input',
          placeholder: '请输入部门简称',
          prop: 'shortName'
        },
        {
          label: '是否业务部门',
          type: 'radio',
          prop: 'isBiz'
        },
        {
          label: '状态',
          type: 'switch',
          prop: 'status',
          active: 0,
          inactive: 1,
          activeText: "开",
          inactiveText: "关"
        },
      ],
      rules: {
        name: [
          { required: true, message: '请输入部门名称', trigger: 'blur' },
        ],
        shortName: [
          { required: true, message: '请输入部门简称', trigger: 'blur' },
        ],
      }
    })
    onBeforeMount(() => {
    })
    onMounted(() => {
    })
    const tableRef = ref()
    const ruleFrom = ref()
    const methods = {
      //表格数据重置
      resetting() {
        data.values = copy(values)
        tableRef.value.resettingTable(data.values)
      },
      //列表根据筛选条件搜索
      searchFor() {
        tableRef.value.handleCurrentChange(1)
      },
      addDept() {
        data.title = '新增部门'
        data.show = true
      },
      //关闭弹窗
      handleClose() {
        ElMessageBox.confirm(
          '当前信息将不再保存?',
          '提示',
          {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
          }
        )
          .then(() => {
            ruleFrom.value.resetFields()//重置表单
            data.show = false
            data.model = copy(model)
          })
      },
      save() {
        ruleFrom.value.verification().then(() => {
          if (!data.model.id) {
            deptAdd(data.model).then(() => {
              Message({ type: 'success', message: '添加成功' })
              tableRef.value.handleCurrentChange(1)//添加表格之后重新获取表格
              data.show = false//关闭弹窗
              ruleFrom.value.resetFields()//重置表单
              data.model = copy(model)//重置表单绑定数据
            })
          } else {
            deptUpdate(data.model).then(() => {
              Message({ type: 'success', message: '修改成功' })
              tableRef.value.handleCurrentChange(1)//添加表格之后重新获取表格
              data.show = false//关闭弹窗
              ruleFrom.value.resetFields()//重置表单
              data.model = copy(model)//重置表单绑定数据
            })
          }
        })
      },
      deleteOut(row: any) {
        if (!row.deleted) {
          Message({ type: 'error', message: '请先禁用该部门，才能进行删除' })
          return
        }
        ElMessageBox.confirm(
          '是否删除当前部门?',
          '提示',
          {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error',
          }
        )
          .then(() => {
            deptDel({ id: row.id }).then(() => {
              Message({ type: 'success', message: '删除成功' })
              tableRef.value.handleCurrentChange(1)//添加表格之后重新获取表格
            })
          })
      },
      edit(row: any) {
        data.title = '编辑部门'
        data.model = objectMatching(row, data.model)
        data.model.status = row.deleted
        data.model.isBiz=String(row.isBiz)
        data.show = true
      },
      //是否禁用当前部门
      changeSwitch(id: number, status: number) {
        if (!id) return
        deptStatus({ id: id, status: status }).then(() => {
          Message({ type: 'success', message: '操作成功' })
        })
      },
     
    }
    return {
      ...toRefs(data),
      ...methods, tableRef,
      Search, Plus, ruleFrom
    }
  }
})
</script>
<style scoped lang='scss'>
.box {
  width: calc(100% - 64px);
  background: #fff;
  box-shadow: 0px 1px 2px 0px rgba(0, 0, 0, 0.1);
  border-radius: 6px;
  height: 70px;
  padding: 10px 32px;
  margin-top: 10px;
}

.mytable {
  overflow-y: auto;
  width: calc(100% - 64px);
  background: #fff;
  box-shadow: 0px 1px 2px 0px rgba(0, 0, 0, 0.1);
  border-radius: 6px;
  padding: 10px 32px;
  height: calc(100% - 125px);
}
</style>
<style lang="scss">
.dept {
  height: calc(100% - 70px);
  overflow-y: auto;
  .el-input {
    width: 100% !important;
  }
}
</style>